package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.bind.GovInfoVo;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;


/**
 * Created by Cooliean on 16/1/19.
 * 绑定信息
 */
public class BindInfoServices extends BaseHttpServices {

    private LoggerHelper loggerHelper = new LoggerHelper(BindInfoServices.class);

    private BindInfoServices(Context context) {
        super(context);
    }

    public static BindInfoServices init() {
        return new BindInfoServices(IApplication.context());
    }

    /**
     * 获取系统所有机构
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public BindInfoServices getAllGov(RefreshType type, int pageSize, int currentPage, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("key_word", StringUtils.nullValue(keyword));
        return this.getAllGov(type, params);
    }

    /**
     * 获取系统所有机构
     *
     * @param params
     * @return
     */
    private BindInfoServices getAllGov(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<GovInfoVo>> callback = new HandlerCallback<List<GovInfoVo>>(EventTag.TAG_GOV_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GOV_LIST);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取系统限制的机构
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public BindInfoServices getScopeGov(RefreshType type, int pageSize, int currentPage, String applicationCode, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("key_word", StringUtils.nullValue(keyword));
        params.put("application_code", StringUtils.nullValue(applicationCode));
        return this.getScopeGov(type, params);
    }

    /**
     * 获取系统限制的机构
     *
     * @param params
     * @return
     */
    private BindInfoServices getScopeGov(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<GovInfoVo>> callback = new HandlerCallback<List<GovInfoVo>>(EventTag.TAG_GOV_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GOV_SCOPE_LIST);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

    /**
     * 获取系统所有企业
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public BindInfoServices getAllCom(RefreshType type, int pageSize, int currentPage, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("key_word", StringUtils.nullValue(keyword));
        return this.getAllCom(type, params);
    }

    /**
     * 获取系统所有企业
     *
     * @param params
     * @return
     */
    private BindInfoServices getAllCom(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<CompanyInfo>> callback = new HandlerCallback<List<CompanyInfo>>(EventTag.TAG_COM_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.COMPANY_LIST_FOR_BIND);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取系统限制的企业
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public BindInfoServices getScopeCom(RefreshType type, int pageSize, int currentPage, String applicationCode, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("key_word", StringUtils.nullValue(keyword));
        params.put("application_code", StringUtils.nullValue(applicationCode));
        return this.getScopeCom(type, params);
    }

    /**
     * 获取系统限制的企业
     *
     * @param params
     * @return
     */
    private BindInfoServices getScopeCom(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<CompanyInfo>> callback = new HandlerCallback<List<CompanyInfo>>(EventTag.TAG_COM_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.COMPANY_SCOPE_LIST_FOR_BIND);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

    /**
     * 保存机构绑定信息
     *
     * @return
     */
    public BindInfoServices saveGovInfoAction(LemonRequestParams params, boolean isEdit) {
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_BIND_SAVE_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(isEdit ? UrlInfo.GOV_EDIT_ACTION : UrlInfo.GOV_NEW_ACTION);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 保存企业绑定信息
     *
     * @return
     */
    public BindInfoServices saveComInfoAction(LemonRequestParams params, boolean isEdit) {
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_BIND_SAVE_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(isEdit ? UrlInfo.BIND_COMPANY_EDIT_ACTION : UrlInfo.BIND_COMPANY_NEW_ACTION);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);

        return this;
    }

    /**
     * 保存绑定信息
     *
     * @return
     */
    public BindInfoServices saveApplicationInfo(LemonRequestParams params) {
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_BIND_SAVE_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.NEW_APPLICATION_INFO);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


}
